<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span class="shanchu" @click="$emit('clear-search-histories', [])"
          >全部删除</span
        >
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow = true" />
    </van-cell>
    <van-cell
      v-for="(item, index) in searchHistories"
      :key="index"
      :title="item"
      @click="onSearchItemClick(item, index)"
    >
      <van-icon v-show="isDeleteShow" name="close" />
    </van-cell>
  </div>
</template>
<script>
export default {
  name: 'SearchHistory',
  components: {},
  props: {
    // Prop数据
    // Prop是受父组件数据影响的
    // 如果是普通数据（数字、字符串、布尔值）绝对不能修改
    // 即便改了也不会传导给父组件
    // 如果是引用类型数据（数组、对象）
    // 可以修改，例如 [].push(x)，对象，xx=x
    // 不能重新赋值，xxx=[]
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      isDeleteShow: false // 控制删除显示状态
    }
  },
  computed: {},
  watch: {},
  methods: {
    onSearchItemClick(item, index) {
      if (this.isDeleteShow) {
        // 删除状态,删除历史记录数据
        this.searchHistories.splice(index, 1)
      } else {
        // 非删除状态，直接进入搜索
        this.$emit('search', item)
      }
    }
  },
  created() {},
  mounted() {}
}
</script>
<style scoped lang="less">
.search-history {
  .shanchu {
    margin-right: 25px;
  }
}
</style>
